package com.neulogistics.contrib.warehouse.infrastructure.persistence.repository;

import com.neulogistics.contrib.warehouse.domain.model.aggregate.Warehouse;
import com.neulogistics.contrib.warehouse.domain.repository.IWarehouseRepository;
import com.neulogistics.contrib.warehouse.infrastructure.persistence.converter.WarehouseEntityPOConverter;
import com.neulogistics.contrib.warehouse.infrastructure.persistence.orm.po.WarehousePO;
import com.neulogistics.contrib.warehouse.infrastructure.persistence.orm.service.IWarehousePOService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class WarehouseRepository implements IWarehouseRepository {

    @Resource
    IWarehousePOService warehousePOService;

    @Override
    public void save(Warehouse warehouse) {
        WarehousePO warehousePO = WarehouseEntityPOConverter.entityToPO(warehouse);
        warehousePOService.saveOrUpdate(warehousePO);
        // 在新增的情况下，需要返回生成的主键
        warehouse.setWarehouseId(warehousePO.getWarehouseId());
    }

    @Override
    public Warehouse findById(Integer id) {
        WarehousePO warehousePO = warehousePOService.getById(id);
        Warehouse warehouse = WarehouseEntityPOConverter.POToEntity(warehousePO);
        return warehouse;
    }

    @Override
    public void deleteById(Integer id) {
        warehousePOService.removeById(id);
    }
}
